﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

public partial class Model事件_EventText2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        evlution evu = new evlution();
        evu.Code = "123123123123";
       evu.ID = "qweqweqweqwe";
      //  evu.Name = "123qweqweqwe";

        Response.Write("<p>" + evu.Code + "</p>");
        Response.Write("<p>" + evu.ID + "</p>");
        Response.Write("<p>" + evu.Name + "</p>");

        TextProperty text = new TextProperty(evu);

        Response.Write("<p>" + text.GetProperty(evu.Code) + "</p>");
        Response.Write("<p>" + text.GetProperty(evu.ID) + "</p>");
        Response.Write("<p>" + text.GetProperty(evu.Name) + "</p>");


        foreach (String Str in evu.PropertyChangeList)
        {
            Response.Write(Str);
        }
    }
}


public class evlutionBase
{
    /// <summary>
    /// 重置变化的属性
    /// </summary>
    public void Reset()
    {
        for (int i = 0; i < Changlist.Length ; i++)
        {
            Changlist[i] = false;
        }
    }
    /// <summary>
    /// 获取属性改变的列
    /// </summary>
    public List<String> PropertyChangeList
    {
        get
        {
            List<String> _propertyChangeList = new List<string>();
            for (int i = 0; i < PropertyNames.Length; i++)
            {
                if (Changlist[i])
                {
                    _propertyChangeList.Add(PropertyNames[i]);
                }
            }
            return _propertyChangeList;
        }
    }
    private bool[] Changlist;
    private String[] _propertyNames;
    /// <summary>
    /// 用于装载对象中的所有属性名
    /// </summary>
    protected String[] PropertyNames
    {
        get
        {
            if (_propertyNames == null)
            {
                this.SetFieldNames();
                Changlist=new  bool[PropertyNames.Length];
            }
            return _propertyNames;
        }
        set { _propertyNames = value; }
    }

    private object[] _propertyValues;
    /// <summary>
    /// 装载对象中的所有属性的值
    /// </summary>
    protected object[] PropertyValues
    {
        get
        {
            if (_propertyValues == null)
            {
                _propertyValues = new object[PropertyNames.Length];
            }
            return _propertyValues;
        }
        set { _propertyValues = value; }
    }
    /// <summary>
    /// 虚方法
    /// 在model中重写
    /// </summary>
    protected virtual void SetFieldNames() { }

    /// <summary>
    /// 设置属性的值
    /// </summary>
    /// <param name="Name">属性名称</param>
    /// <param name="Value">属性对应的值</param>
    protected void SetProperty(String Name, object Value)
    {
        for (int i = 0; i < PropertyNames.Length; i++)
        {
            if (PropertyNames[i].ToLower() == Name.ToLower())
            {
                //这里添加值变化事件 
                Changlist[i] = true;
                PropertyValues[i] = Value;
                break;
            }
        }
    }

    private T ConvertType<T>(object obj)
    {
        if (obj != null)
            return (T)obj;
        else if (obj == DBNull.Value || obj == null)
        {
            if (typeof(T) == typeof(DateTime))
            {
                object o = DateTime.MinValue;
                return (T)o;
            }
            else
                return default(T);
        }
        else
        {
            //如果 Value为 decimal类型，Ｔ　为double 类型， (T)Value 将发生错误
            return (T)Convert.ChangeType(obj, typeof(T));
        }
    }
    protected object GetPropertyValue(String Name)
    {
        for (int i = 0; i < PropertyNames.Length; i++)
        {
            if (String.Compare(PropertyNames[i], Name, true) == 0)
            {
                return PropertyValues[i];
            }
        }
        return null;
    }
    /// <summary>
    /// 获取属性的值
    /// </summary>
    /// <typeparam name="T">属性类型</typeparam>
    /// <param name="Name">属性名称</param>
    /// <returns>对应属性类型的值</returns>
    protected T GetProperty<T>(String Name)
    {
        //这里添加属性变化事件
        OnPropertyNameGetting(Name);
        object obj = GetPropertyValue(Name);
        return ConvertType<T>(obj);
    }

    public event EventHandler<PropertyNameGettingEventArgs> PropertyNameGetting;
    public virtual void OnPropertyNameGetting(String Name)
    {
        if (this.PropertyNameGetting != null)
        {
            this.PropertyNameGetting(this, new PropertyNameGettingEventArgs(Name));
        }
    }
}

public class PropertyNameGettingEventArgs : EventArgs
{
    private String _propertyName;
    public String PropertyName
    {
        get { return _propertyName; }
        set { _propertyName = value; }
    }

    //构造函数
    public PropertyNameGettingEventArgs(String Name)
    {
        this.PropertyName = Name;
    }
}


public class evlution : evlutionBase
{
    protected override void SetFieldNames()
    {
        PropertyNames = new String[] { "ID", "Name", "Code" };
    }

    public String ID
    {
        get { return GetProperty<String>("ID"); }
        set { SetProperty("ID", value); }
    }

    public String Name
    {
        get { return GetProperty<String>("Name"); }
        set { SetProperty("Name", value); }
    }

    public String Code
    {
        get { return GetProperty<String>("Code"); }
        set { SetProperty("Code", value); }
    }
}



public class TextProperty
{
    public String PropertyName { get; set; }

    public TextProperty(evlutionBase e)
    {
        e.PropertyNameGetting += new EventHandler<PropertyNameGettingEventArgs>(OnPropertyNameGetting);
    }

    public void OnPropertyNameGetting(object sender, PropertyNameGettingEventArgs e)
    {
        PropertyName = e.PropertyName;
    }

    public string GetProperty(String Name)
    {
        return PropertyName;
    }
}







